package com.ruoyi.elebook.controller;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.file.FileTypeUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.elebook.domain.BookPage;
import com.ruoyi.elebook.domain.BookPageVo;
import com.ruoyi.elebook.service.IBookElebookService;
import com.ruoyi.elebook.service.IBookMpElebookService;
import com.ruoyi.elebook.service.IBookPageService;
import com.ruoyi.mp.api.RemoteMpCodeFactoryService;
import com.ruoyi.mp.api.domain.MpGetCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 图片页Controller
 * 
 * @author ruoyi
 * @date 2023-12-11
 */
@RestController
@RequestMapping("/page")
public class BookPageController extends BaseController
{
    @Autowired
    private IBookPageService bookPageService;
    @Autowired
    private IBookElebookService bookElebookService;
    @Autowired
    private RemoteMpCodeFactoryService remoteMpCodeFactoryService;
    @Autowired
    private IBookMpElebookService iBookMpElebookService;
    /**
     * 资源映射路径 前缀
     */
    @Value("${url.path}")
    public String localFilePrefix;
    /**
     * 查询图片页列表
     */
    @RequiresPermissions("elebook:page:list")
    @GetMapping("/list")
    public TableDataInfo list(BookPage bookPage)
    {
        startPage();
        List<BookPage> list = bookPageService.selectBookPageList(bookPage);
        return getDataTable(list);
    }

    @PostMapping("/getCodeUrl")
    public String getCodeUrl(BookPage bookPage){
        Integer  elebook_id = this.bookElebookService.selectStatus(localFilePrefix+bookPage.getAccessUrl());
        if(elebook_id != null){
            try {
                MpGetCode mpGetCode = new MpGetCode();
                int elebookId = bookPage.getElebookId().intValue();
                int time = 3600;
                Integer mp_id = this.iBookMpElebookService.selectBookMpElebookByElebookId(elebookId);
                mpGetCode.setMpId(mp_id);
                mpGetCode.setSeesionid(bookPage.getSeesionid());
                mpGetCode.setTime(time);
                R<String> mpResult = remoteMpCodeFactoryService.mpGetcodeUrl(mpGetCode, SecurityConstants.INNER);
                return mpResult.getData();
            }catch (Exception e){
                throw new RuntimeException(e);
            }
        }else {
          return  null;
        }
    }


    /**
     * 查询图片页列表
     */
    @GetMapping("/readListPage")
    public TableDataInfo readListPage(BookPage bookPage)
    {


        Integer  elebook_id = this.bookElebookService.selectStatus(localFilePrefix+bookPage.getAccessUrl());
        if(elebook_id != null){
            startPage();
            bookPage.setAccessUrl(null);
            bookPage.setElebookId((long) elebook_id);
            List<BookPage> list = bookPageService.selectBookPageList(bookPage);
           for(BookPage bookPages : list){
               bookPages.setPageName(null);
               bookPages.setCreateBy(null);
               bookPages.setElebookId(null);
               bookPages.setId(null);
           }
            return getDataTable(list);
        }else {
            List<BookPage> list  = new ArrayList<>();
            return getDataTable(list);
        }
    }


    /**
     * 导出图片页列表
     */
    @RequiresPermissions("elebook:page:export")
    @Log(title = "图片页", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BookPage bookPage)
    {
        List<BookPage> list = bookPageService.selectBookPageList(bookPage);
        ExcelUtil<BookPage> util = new ExcelUtil<BookPage>(BookPage.class);
        util.exportExcel(response, list, "图片页数据");
    }

    /**
     * 获取图片页详细信息
     */
    @RequiresPermissions("elebook:page:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bookPageService.selectBookPageById(id));
    }

    /**
     * 新增图片页
     */
    @RequiresPermissions("elebook:page:add")
    @Log(title = "图片页")
    @RequestMapping("/add")
    public AjaxResult add( BookPageVo bookPageVo)
    {
            if (!bookPageVo.getFile().isEmpty())
            {
                String extension = FileTypeUtils.getExtension(bookPageVo.getFile());
                if(extension.equals("png") || extension.equals("jpg")){
                    return toAjax(bookPageService.insertBookPage(bookPageVo));
                }else{
                    return error("文件格式不正确");
                }
            }else{
                return error("文件为null");
            }
    }

    /**
     * 修改图片页
     */
    @RequiresPermissions("elebook:page:edit")
    @Log(title = "图片页", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BookPage bookPage)
    {
        return toAjax(bookPageService.updateBookPage(bookPage));
    }

    /**
     * 删除图片页
     */
    @RequiresPermissions("elebook:page:remove")
    @Log(title = "图片页", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bookPageService.deleteBookPageByIds(ids));
    }


    /**
     * 修改图片页首页
     */
    @RequiresPermissions("elebook:page:edit")
    @Log(title = "图片页")
    @RequestMapping("/updateCover")
    public AjaxResult updateCover(@RequestBody BookPage bookPage)
    {
        return toAjax(bookPageService.updateCover(bookPage));
    }

}
